package com.sduept.nwld.dataserver.controller.equipment.assessment;

import com.sduept.bigdata.equipment.assessment.enums.ItemTypeEnum;
import com.sduept.bigdata.equipment.assessment.model.standardlib.StandardInspection;
import com.sduept.bigdata.equipment.assessment.service.StandardInspectService;
import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.nwld.dataserver.util.LibTreeNode;
import org.primefaces.PrimeFaces;
import org.primefaces.event.NodeExpandEvent;
import org.primefaces.event.NodeSelectEvent;
import org.primefaces.event.NodeUnselectEvent;
import org.primefaces.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import java.util.*;

/**
 * 评价标准多选择器controller
 * @author huangyu
 * @Date 2019年7月31日
 */
@Named
@ViewScoped
public class StandardSelectorController extends AbstractController {

	private static final long serialVersionUID = 6967192575583328458L;
	@Autowired
	private StandardInspectService service;
	
	private TreeNode[] selectedNodes;
	/**
	 * 接收的参数
	 */
	private String[] selectedStands;
	
	private TreeNode treeRoot;
	/**
	 * 选中父节点后新建子节点，为方便确定子节点的类型，将枚举类型放入list中
	 */
	private List<ItemTypeEnum> typeList;
	
	
	@PostConstruct
	public void init() {
		selectedStands = getParameters("selectedStands");
		initStaticTree();
		typeList = Arrays.asList(ItemTypeEnum.ROOTNAME,ItemTypeEnum.INSPECTIONPROJECT,ItemTypeEnum.INSPECTIONITME,ItemTypeEnum.INSPECTIONREQUIREMENT,ItemTypeEnum.INSPECTIONCRITERION);
	}
	
	/**
	 * 初始化树，仅展示当前使用的版本
	 */
	private void initDynamicTree() {
		treeRoot = new LibTreeNode("Root", null);
		// 获取当前版本的根节点
		try {
			List<StandardInspection> rootStands = service.findAvailableRoot();
			if(rootStands.size()>0) {
				StandardInspection rootStand = rootStands.get(0);
				TreeNode pNode = new LibTreeNode(ItemTypeEnum.ROOTNAME.getValue(),rootStand, treeRoot);
				if(selectedStands!=null) {
					for (int i=0;i<selectedStands.length;i++) {
						if(rootStand.getId().equals(selectedStands[i])) {
							pNode.setSelected(true);
						}
					}
				}
				List<StandardInspection> lt = service.findByParentId(rootStand.getId());
				service.sort(lt);
				for (StandardInspection stand : lt) {
					TreeNode node = new LibTreeNode(ItemTypeEnum.INSPECTIONPROJECT.getValue(),stand, pNode);
					// 如果父节点被选中，子也被选中
					if(pNode.isSelected()) {
						node.setSelected(true);
					} else {
						if(selectedStands!=null) {
							for (int i=0;i<selectedStands.length;i++) {
								if(stand.getId().equals(selectedStands[i])) {
									node.setSelected(true);
								}
							}
						}
					}
				}
				pNode.setExpanded(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
			addErrorMessage("获取可用版本数据过程中发生错误");
		}
	}
	
	/**
	 * 初始化树，构造所有子节点
	 */
	private void initStaticTree() {
		treeRoot = new LibTreeNode("Root", null);
		// 获取当前版本的根节点
		try {
			List<StandardInspection> rootStands = service.findAvailableRoot();
			for (StandardInspection rootStand : rootStands) {
				recursionGenStaticTree(treeRoot, rootStand);
				treeRoot.setExpanded(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
			addErrorMessage("获取可用版本数据过程中发生错误");
		}
	}
	// 递归构造树，构造所有节点
	private void recursionGenStaticTree(TreeNode pNode,StandardInspection stand) {
		if(null == stand) {
			return ;
		}
		pNode.setExpanded(true);
		TreeNode node = new LibTreeNode(stand.getItemType().getValue(),stand, pNode);
		if(pNode.isSelected()) {
			node.setSelected(true);
		} else {
			if(selectedStands!=null) {
				for (int i=0;i<selectedStands.length;i++) {
					if(stand.getId().equals(selectedStands[i])) {
						node.setSelected(true);
					}
				}
			}
		}
		List<StandardInspection> lt = service.findByParentId(stand.getId());
		for (StandardInspection standard : lt) {
			recursionGenStaticTree(node, standard);
		}
	}

	/**
	 * 树节点展开
	 * @param event
	 */
	public void onNodeExpand(NodeExpandEvent event) {
		TreeNode node = event.getTreeNode();
		node.getChildren().clear();
		StandardInspection select = (StandardInspection) node.getData();
		List<StandardInspection> list = service.findByParentId(select.getId());
		service.sort(list);
		ItemTypeEnum type = null;
		for (StandardInspection stand : list) {			
			type = getChildrenTypeByParent(select);
			if(null != type) {
				TreeNode pnode = new LibTreeNode(type.getValue(),stand, node);
				// 如果父节点被选中，子也被选中
				if(node.isSelected()) {
					pnode.setSelected(true);
				} else {
					if(selectedStands!=null) {
						for (int i=0;i<selectedStands.length;i++) {
							if(stand.getId().equals(selectedStands[i])) {
								pnode.setSelected(true);
							}
						}
					}
				}
			}
		}
	}
	
	/**
	 * 根据父获取子的类型
	 * @param parent
	 * @return ItemTypeEnum
	 */
	private ItemTypeEnum getChildrenTypeByParent(StandardInspection parent) {
		if(null == parent) {
			return null;
		}
		ItemTypeEnum type = null;
		int index = typeList.indexOf(parent.getItemType());
		if((index+1) < typeList.size()) {
			type = typeList.get(index+1);
		}
		return type;
	}
	
	public void selectStandards(){
		if(null == selectedNodes) {
			PrimeFaces.current().dialog().closeDynamic(null);
			return;
		}
		List<StandardInspection> selects = new ArrayList<StandardInspection>();
		for(TreeNode tn : selectedNodes){
			StandardInspection stand = (StandardInspection) tn.getData();
			selects.add(stand);
		}
		PrimeFaces.current().dialog().closeDynamic(selects);
	}
	
	/**
	 * 选择树节点
	 */
	public void onStandardNodeSelect(NodeSelectEvent event) {
	}
	
	/**
	 * 取消选择树节点
	 * @param event
	 */
	public void onStandardNodeUnSelect(NodeUnselectEvent event) {
	}
	
	/**
	 * 打开评价标准选择框
	 */
	public void openStandardSelectorDialog() {
		Map<String, Object> options = new HashMap<String, Object>();
		options.put("resizable", true);
		options.put("draggable", true);
		options.put("modal", true);
		options.put("width", 620);
		options.put("height", 650);
		Map<String, List<String>> params = new HashMap<>();
		params.put("selectedStands", null);
		PrimeFaces.current().dialog().openDynamic("/ui/common/standard_selector_multiple.xhtml",  options,params);
	}

	public TreeNode[] getSelectedNodes() {
		return selectedNodes;
	}

	public void setSelectedNodes(TreeNode[] selectedNodes) {
		this.selectedNodes = selectedNodes;
	}

	public TreeNode getTreeRoot() {
		return treeRoot;
	}

	public void setTreeRoot(TreeNode treeRoot) {
		this.treeRoot = treeRoot;
	}
}
